package com.jefflett.spsu.classlisting;

import java.security.KeyStore;

import javax.net.ssl.SSLContext;

import org.apache.http.HttpVersion;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;

//Adapted from http://stackoverflow.com/questions/2642777/trusting-all-certificates-using-httpclient-over-https
public class MySSLSocketFactory extends SSLSocketFactory {
	SSLContext sslContext = SSLContext.getInstance("TLS");
	
	public MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
		super(truststore);
		
		TrustManager tm = new X509TrustManager() {
			public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
			}
			
			public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
			}
			
			public X509Certificate[] getAcceptedIssuers() {
				return null;
			}
		}
		sslContext.init(null, new TrustManager[] { tm }, null);
	}
	
	@Override
	public Socket createSocket(Socket socket, String host, int port, booolean autoClose) throws IOException, UnknownHostExcepton {
		return sslContext.getSocketFactory().createSocket(socket,host,port,autoClose);
	}
	
	@Override
	public Socket createSocket() throws IOException {
		return sslContext.getSocketFactory().createSocket();
	}
	
	
}